package net.asgarli.abalone.model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Class the indicates the version of something. Contains major and minor
 * versions. For example 2.11 where 2 will be major version and 11 minor
 * version.
 * 
 * @author Elchin
 * @since 0.1
 * @version 0.1
 */
public class VersionInfo implements Marshallable {
    private static final long serialVersionUID = 1L;

    private int majorVersion;

    private int minorVersion;

    /** Default constructor. */
    public VersionInfo() {}
    
    /**
     * Constructor that takes major and minor versions as parameter.
     * 
     * @param majorVersion
     * @param minorVersion
     */
    public VersionInfo(int majorVersion, int minorVersion) {
        this.majorVersion = majorVersion;
        this.minorVersion = minorVersion;
    }

    public int getMajorVersion() {
        return majorVersion;
    }

    public int getMinorVersion() {
        return minorVersion;
    }

    /**
     * Returns the String representation of object in format
     * majorVersion.minorVersion
     */
    @Override
    public String toString() {
        return majorVersion + "." + minorVersion;
    }

    @Override
    public byte[] writeToBytes() throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeInt(majorVersion);
        dos.writeInt(minorVersion);
        return bos.toByteArray();
    }

    @Override
    public void restoreFromBytes(byte[] bytes) throws IOException {
        DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
        majorVersion = in.readInt();
        minorVersion = in.readInt();
    }

    //
    // ************************* GENERATED *************************
    //
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + majorVersion;
        result = prime * result + minorVersion;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (!(obj instanceof VersionInfo)) return false;
        VersionInfo other = (VersionInfo) obj;
        if (majorVersion != other.majorVersion) return false;
        if (minorVersion != other.minorVersion) return false;
        return true;
    }
}
